package com.wing.sell.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wing.product.service.DynamicService;
import com.wing.sell.dao.OrderOfferDao;
import com.wing.sell.model.entity.OrderOffer;
import com.wing.sell.model.entity.OrderOfferDetail;
import com.wing.sell.model.entity.OrderPro;
import com.wing.sell.model.request.OrderOfferForm;
import com.wing.sell.model.response.OrderOfferVO;
import com.wing.sell.model.response.OrderProExcelDetailVo;
import com.wing.sell.model.response.OrderProVO;
import com.wing.sell.service.OrderOfferDetailService;
import com.wing.sell.service.OrderOfferService;
import com.wing.sell.service.OrderProService;
import com.wing.sell.service.OrderService;
import com.wing.web.utils.JwtUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.time.Duration;
import java.time.LocalDateTime;
import java.time.temporal.ChronoUnit;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 销售订单产品报价记录表-ServiceImpl
 *
 * @author
 */
@Service
@Slf4j
public class OrderOfferServiceImpl extends ServiceImpl<OrderOfferDao, OrderOffer> implements OrderOfferService {

    @Autowired
    private DynamicService dynamicService;
    @Autowired
    private OrderOfferDetailService orderOfferDetailService;
    @Autowired
    private OrderProService orderProService;
    @Autowired
    private OrderService orderService;


    @Override
    @Transactional
    public void add(OrderOfferForm orderOfferForm) {

        //校验

        OrderOffer orderOffer =new OrderOffer();
        CopyOptions copyOptions = CopyOptions.create()
        .setEditable(OrderOffer.class)
        .setIgnoreError(true)
        .setIgnoreNullValue(true);
        BeanUtil.copyProperties(orderOfferForm, orderOffer, copyOptions);
        SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyyMMdd");
        String code=dynamicService.getDayNextCode("sel_order_offer");
        String day=simpleDateFormat.format(new Date());
        orderOffer.setCode("BJ"+day+code);
        orderOffer.setDay(Integer.valueOf(day));
        orderOffer.setNo(Integer.valueOf(code));
        orderOffer.setProNum(orderOffer.getOrderOfferDetailList().size());
        //添加报价单时  更新时长取生成订单的时间与生成报价单的时间差
        LocalDateTime currentTime = orderService.get4appById(orderOfferForm.getOrderId()).getCreateTime();
        LocalDateTime offWorkTime = LocalDateTime.now();
        LocalDateTime tempDateTime = LocalDateTime.from(currentTime);
        long days = tempDateTime.until(offWorkTime, ChronoUnit.DAYS);
        tempDateTime = tempDateTime.plusDays(days);
        long hours = tempDateTime.until(offWorkTime, ChronoUnit.HOURS);
        tempDateTime = tempDateTime.plusHours(hours);
        long minutes = tempDateTime.until(offWorkTime, ChronoUnit.MINUTES);
        tempDateTime = tempDateTime.plusMinutes(minutes);
        long seconds = tempDateTime.until(offWorkTime, ChronoUnit.SECONDS);
        orderOffer.setUpdateDuring("" + days + " 天 " + hours + " 小时 " + minutes + " 分 " + seconds + " 秒");
        baseMapper.insert(orderOffer);

        BigDecimal totalMoney = new BigDecimal(0);
    //一个产品只会在一个报价单里面，只会新增一次 后续都在报价单里面修改
        for(OrderOfferDetail orderOfferDetail:orderOffer.getOrderOfferDetailList()){
            orderOfferDetail.setOfferId(orderOffer.getId());
            orderOfferDetail.setOrderId(orderOfferForm.getOrderId());
            orderOfferDetail.setLastMoney(orderOfferDetail.getOfferMoney());
            orderOfferDetailService.save(orderOfferDetail);

            //添加到报价单后的产品 产品表的报价状态修改为报价中
            OrderProVO orderProVO =  orderProService.get4appById(orderOfferDetail.getProId());
            OrderPro orderPro =new OrderPro();
            CopyOptions copy = CopyOptions.create()
                    .setEditable(OrderPro.class)
                    .setIgnoreError(true)
                    .setIgnoreNullValue(true);
            BeanUtil.copyProperties(orderProVO, orderPro, copy);
            orderPro.setOfferStatus(OrderPro.OfferStatus.running);
            orderProService.updateById(orderPro);
            totalMoney = totalMoney.add(orderOfferDetail.getOfferMoney().multiply(new BigDecimal(orderProVO.getQuantity())));//报价单总金额
        }
        OrderOffer orderOfferUpdate = baseMapper.selectOne(new QueryWrapper<OrderOffer>().eq("id",orderOffer.getId()));
        orderOfferUpdate.setTotalMoney(totalMoney);
        baseMapper.updateById(orderOfferUpdate);
    }

    @Override
    public List<OrderOfferVO> list4app(Map<String, Object> params) {
        return baseMapper.list4app(params);
    }

    @Override
    public Page<OrderOfferVO> pageList4app(Page page, Map<String, Object> params) {
        List<OrderOfferVO> list = baseMapper.list4app(page, params);
        page.setRecords(list);
        return page;
    }

    @Override
    public OrderOfferVO get4appById(Long id) {
        return baseMapper.get4appById(id);
    }

    /**
     * 获取产品的最终价格
     * @param orderId
     * @param id
     * @return
     */
    @Override
    public BigDecimal getOffMoneyBy(Long orderId, Long id) {
        return baseMapper.getOffMoneyBy(orderId,id);
    }
    // BigDecimal getOffMoneyByProductDetailId(@Param("orderId") Long orderId,@Param("proId") Long id);


    /**
     * 获取产品的最终价格
     * @param orderId
     * @param id
     * @return
     */
    @Override
    public BigDecimal getOffMoneyByProductDetailId(Long orderId, Long id) {
        return baseMapper.getOffMoneyByProductDetailId(orderId,id);
    }

    /**
     * 正式订单导出excel
     * @param params
     * @return
     */
    @Override
    public List<OrderProExcelDetailVo> exportInProductInfoDetail(Map<String, Object> params) {
        return baseMapper.exportInProductInfoDetail(params);
    }
}
